package com.zt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.Result;
import com.zt.pojo.Permissions;
import com.zt.pojo.Roles;
import com.zt.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class PermissionsController {
    @Autowired
    private PermissionsService permissionsService;

    /**
     * 添加权限
     * @param permissions
     * @return
     */
    @PostMapping("/permissions")
    public Result addPermissions(@RequestBody Permissions permissions){
        if (permissionsService.getOne(new QueryWrapper<Permissions>().lambda().eq(Permissions::getName,permissions.getName()))!=null){
            return Result.ERROR("该权限已存在");
        }
        boolean save = permissionsService.save(permissions);
        return save?Result.OK():Result.ERROR();
    }

    /**
     * 获取权限列表
     * @return
     */
    @GetMapping("/permissions")
    public Result permissions(){
        return Result.OK(permissionsService.list());
    }

    /**
     * 修改权限
     * @param permissions
     * @param id
     * @return
     */
    @PutMapping("/permissions/{id}")
    public Result updateRole(@RequestBody Permissions permissions, @PathVariable Integer id){
        Permissions byId = permissionsService.getById(id);
        boolean update=false;
        if (byId!=null){
            update=permissionsService.updateById(permissions);
        }
        return update?Result.OK():Result.ERROR();
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/permissions/{id}")
    public Result delete(@PathVariable Integer id){
        Permissions byId = permissionsService.getById(id);
        boolean delete=false;
        if (byId!=null){
            delete=permissionsService.removeById(byId);
        }
        return delete?Result.OK():Result.ERROR();
    }
}
